<template>
  <div>
    <!-- <div style="font-size:32px;position:fixed;top:180px;padding:10px" @click="handleData"> <a href="javascript:;">开始</a> </div> -->
  </div>

</template>

<script>
import { h3CompanyInfo, h3CompanyInfoEdit } from '@/api/home'
import community from '@/views/layout/community'
// import companyData from '@/data/h3_company_info.json'
export default {
  data() {
    return {
      tableList: [],
      page: 0,
      size: 20,
      communityOptions: Object.values(community)
      // ?page=0&size=10&sort=id%2Cdesc
    }
  },
  watch: {
    '$route.query': {
      handler(newVal) {
        this.startSearch()
      },
      deep: true,
      immediate: true
    }
  },
  created() {

  },
  mounted() {
    this.$eventBus.$on('mapCreated', this.handleMapCreated)
  },
  methods: {
    startSearch() {
      if (!this.$eventBus.autocomplete) {
        setTimeout(this.startSearch, 200)
        return
      }
      const address = this.$route.query.address
      const addressStr = address.replace(/\d*室.*|号.*|^浙江省杭州市滨江区/g, '')
      this.$eventBus.autocomplete.emit('select', { poi: { name: addressStr }})
      // this._placeSearch.search(this.$route.query.address, (status, result) => {
      //   // 查询成功时，result即对应匹配的POI信息
      //   console.log(result)
      //   var pois = result.poiList.pois
      //   let position = null
      //   for (var i = 0; i < pois.length; i++) {
      //     var poi = pois[i]
      //     var marker = []
      //     marker[i] = new this._AMap.Marker({
      //       position: poi.location, // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
      //       title: poi.name,
      //       map: this._mapInstance
      //     })
      //     position = poi.location
      //   }
      //   this._mapInstance.setCenter(position)
      // })
      // this._placeSearch.emit('select', { poi: { name: this.$route.query.address }})
    },
    handleMapCreated(mapInstance, placeSearch, _AMap) {
      this._mapInstance = mapInstance
      this._placeSearch = placeSearch
      this._AMap = _AMap
      // this._placeSearch = new this._AMap.PlaceSearch({ city: 'hangzhou', citylimit: true })
      // this._placeSearch.search(this.$route.query.address)

      // console.log(placeSearch)
    },
    sleep(d) {
      for (var t = Date.now(); Date.now() - t <= d;);
    },
    handleData() {
      // if (this.page === companyData.length / 2) {
      //   console.log(JSON.stringify(companyData))
      // }
      // this.sleep(100)
      // const placeSearch = new this._AMap.PlaceSearch({ city: 'hangzhou', citylimit: true })
      // const tableData = companyData[this.page]
      // const targetStr = tableData.dz.replace(/\d*室.*|号.*|^浙江省杭州市滨江区/g, '')
      // placeSearch.search(targetStr, (status, result) => {
      //   if (status === 'no_data' || !result.poiList) {
      //     console.log(`根据${targetStr}查询不到信息，以跳过 `)
      //     this.page++
      //     this.handleData()
      //     return
      //   }
      //   console.log(`根据${targetStr}查询到${JSON.stringify(result.poiList.pois[0].location)}`)
      //   tableData.location = JSON.stringify(result.poiList.pois[0].location)
      //   console.log(`第${this.page}条存入内存,开始执行下一条`)
      //   localStorage.setItem('')
      //   this.page++
      //   this.handleData()
      // })
      h3CompanyInfo({ page: this.page, size: 1 }).then(res => {
        const content = res.content[0]
        if (!content) return
        this._placeSearch = new this._AMap.PlaceSearch({ city: 'hangzhou', citylimit: true })
        const targetStr = content.dz.replace(/\d*室.*|号.*|^浙江省杭州市滨江区/g, '')
        this._placeSearch.search(targetStr, (status, result) => {
          if (status === 'no_data' || !result.poiList) {
            console.log(`根据${content.dz}查询不到信息，以跳过 `)
            this.page++
            this.handleData()
            return
          }
          console.log(`根据${content.dz}查询到${JSON.stringify(result.poiList.pois[0].location)}`)

          content.location = JSON.stringify(result.poiList.pois[0].location)
          h3CompanyInfoEdit(content).then(res => {
            console.log(`编号：${this.page}企业：${content.location}--跟新成功`)
            setTimeout(() => {
              this.page++
              this.handleData()
            }, 50)
          })
        })
      })
    }
  }
}
</script>
